//
//  TitleButton.m
//  微博2期
//
//  Created by J on 15/11/28.
//
//

#import "TitleButton.h"
#define Margin 10
@implementation TitleButton

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
//        self.backgroundColor = [UIColor purpleColor];
        [self setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];

    }
    
    return self;
}
//目的: 想在系统计算和设置完按钮的尺寸后, 再修改一下尺寸  不论调用什么方法目的都是修改frame 干脆重写frame 调用layoutSubviews最终也会调用这个方法
//如果想在系统设置完控件的尺寸后,再做修改,而且保证修改成功,一般都在setFrame:中设置
- (void)setFrame:(CGRect)frame{
    frame.size.width += Margin;
    [super setFrame:frame];

    
    
}
- (void)layoutSubviews{//自己的尺寸被改的时候调用 如果在里面改自己的尺寸会死循环
    [super layoutSubviews];
    
    //如果仅仅是调整按钮内部titleLabel和imageView位置 那么我们在layoutSubviews中单独设置位置即可
    //计算titleLabel的frame
    self.titleLabel.x = self.imageView.x;
    
    //计算imageView的frame
    self.imageView.x = CGRectGetMaxX(self.titleLabel.frame)+Margin;//MaxX ＝ x+width
    

}
- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    
    [super setTitle:title forState:state];
    [self sizeToFit];

}


- (void)setImage:(UIImage *)image forState:(UIControlState)state{
    
    [super setImage:image forState:state];
    [self sizeToFit];

    
}

@end
